package vip.xiaonuo.sys.fymodule.account.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.util.Map;

@Slf4j
public class HttpUtils {

    private static final PoolingHttpClientConnectionManager HTTP_CLIENT_CONNECTION_MANAGER;

    private static final CloseableHttpClient HTTP_CLIENT;
    private final static String CHARSET_DEFAULT = "UTF-8";
    static {
        HTTP_CLIENT_CONNECTION_MANAGER = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build());
        HTTP_CLIENT_CONNECTION_MANAGER.setDefaultMaxPerRoute(100);
        HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(200);
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000).setSocketTimeout(60000).build();
        HTTP_CLIENT = HttpClientBuilder.create().setConnectionManager(HTTP_CLIENT_CONNECTION_MANAGER).setDefaultRequestConfig(requestConfig).build();
    }


    public static HttpResult doPostJson(String url, Header header, Map<String, Object> parameterMap) {
        HttpResult result = new HttpResult();
        try {
            HttpPost httpPost = new HttpPost(url);
            if (header != null) {
                httpPost.setHeader(header);
            }
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity reqEntity = new StringEntity(JSON.toJSONString(parameterMap), CHARSET_DEFAULT);
            reqEntity.setContentEncoding(CHARSET_DEFAULT);
            reqEntity.setContentType("application/json");
            httpPost.setEntity(reqEntity);
            CloseableHttpResponse resp = HTTP_CLIENT.execute(httpPost);
            HttpEntity httpEntity = null;
            try {
                httpEntity = resp.getEntity();
                String body = EntityUtils.toString(httpEntity, CHARSET_DEFAULT);
                int statusCode = resp.getStatusLine().getStatusCode();
                result.setStatus(statusCode);
                result.setBody(body);
                return result;
            } finally {
                EntityUtils.consume(httpEntity);
                resp.close();
            }
        } catch (Exception e) {
            log.error("post:{}",e.getMessage());
        }
        return null;
    }



}
